﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the shopping cart entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  | - Items(of the Extended level)
    ///  + Extended
    ///   | - Options(of the Extended level)
    ///   + Base
    ///    | - Customer(of the Base level)
    ///    | - Total
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class ShoppingCartEntity : AuditableEntity,
        IRelationHolder<SalesOptionEntity>
    {
        #region Fields

        private ICollection<ShoppingCartItemEntity> _items;
        private ICollection<SalesOptionEntity> _options;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public ShoppingCartEntity()
        {
            _items = new List<ShoppingCartItemEntity>();
            _options = new List<SalesOptionEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or set the customer.
        /// </summary>
        public virtual CustomerEntity Customer
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the total.
        /// </summary>
        public virtual decimal Total
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual ShoppingCartFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections

        /// <summary>
        /// Gets or sets the item collection.
        /// </summary>
        public virtual ICollection<ShoppingCartItemEntity> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
            }
        }

        /// <summary>
        /// Gets or sets the option collection.
        /// </summary>
        public virtual ICollection<SalesOptionEntity> Options
        {
            get
            {
                return _options;
            }
            set
            {
                _options = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Maps an option to the cart.
        /// </summary>
        /// <param name="child">The option to map.</param>
        public virtual void Map(SalesOptionEntity child)
        {
            if(Options.Contains(child))
                return;
            Options.Add(child);
        }

        /// <summary>
        /// Unmaps the option from the cart.
        /// </summary>
        /// <param name="child">The option to unmap.</param>
        public virtual void Unmap(SalesOptionEntity child)
        {
            if(!Options.Contains(child))
                return;
            Options.Remove(child);
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Customer"] = Customer.Disclose(DisclosureLevel.Base, options);
                obj["Total"] = Total;
                obj["Flags"] = Flags.ToString();
            }
            if(level >= DisclosureLevel.Extended)
                obj["Options"] = Options.Map(x => x.Disclose(DisclosureLevel.Extended, options));
            if(level >= DisclosureLevel.Entire)
                obj["Items"] = Items.Map(x => x.Disclose(DisclosureLevel.Extended, options));
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class ShoppingCartMapping : EntityMapping<ShoppingCartEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public ShoppingCartMapping()
        {
            Table("fwk_ShoppingCarts");
            Id(x => x.EntityID, m =>
            {
                m.Column("CartID");
                m.Generator(Generators.Identity);
            });
            ManyToOne(x => x.Customer, m =>
            {
                m.Column("CustomerID");
                m.ForeignKey("FK_fwk_ShoppingCarts_fwk_Customers");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.Total, m =>
            {
                m.Precision(19);
                m.Scale(4);
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Items, m =>
            {
                m.Table("fwk_ShoppingCartItems");
                m.Key(k =>
                {
                    k.Column("CartID");
                    k.ForeignKey("FK_fwk_ShoppingCartItems_fwk_ShoppingCarts");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
            Bag(x => x.Options, m =>
            {
                m.Table("fwk_MShoppingCartOptions");
                m.Key(k =>
                {
                    k.Column("CartID");
                    k.ForeignKey("FK_fwk_MShoppingCartOptions_fwk_ShoppingCarts");
                    k.NotNullable(true);
                });
            },
            a =>
            {
                a.ManyToMany(m =>
                {
                    m.Column("OptionID");
                    m.ForeignKey("FK_fwk_MShoppingCartOptions_fwk_SalesOptions");
                });
            });
        }
    }
}
